package com.event.usc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.event.usc.dto.UserEvent;
import com.event.usc.oracleJDBC.DatabaseHandler;
import com.google.gson.Gson;

@WebServlet("/InviteServlet")
public class InviteServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String qtype = request.getParameter("q");
		long eventid = Long.parseLong(request.getParameter("eventid"));
		if (qtype.equals("all"))
			doAllRequest(eventid, response);
		else
		{
			String userid = request.getParameter("userid");
			if (qtype.equals("allfd"))
				doAllFdRequest(eventid, userid, response);
			else if (qtype.equals("allfdnc"))
				doAllFdNcRequest(eventid, userid, response);
			else if (qtype.equals("checkin"))
			{
				String comment = request.getParameter("comment");
				doCheckIn(eventid, userid, comment, response);
			}
			else if (qtype.equals("checkout"))
				doCheckOut(eventid, userid, response);
			else if (qtype.equals("invite"))
			{
				String[] friendid = request.getParameterValues("friendid");
				doInviteRequest(eventid, userid, friendid, response);
			}
		}
	}

	private void doCheckOut(long eventid, String userid,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();         
			printResponse(response, handler.insertCheckOut(eventid, userid));	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void doCheckIn(long eventid, String userid, String comment,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();         
			printResponse(response, handler.insertCheckIn(eventid, userid, comment));	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void doInviteRequest(long eventid, String userid, String[] friendid,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();         
			printResponse(response, handler.insertInvite(eventid, userid, friendid));	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void printResponse(HttpServletResponse response,
			boolean insertInvite) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        String output = insertInvite? "{'status':'success'}":"{'status':'error'}";
        out.write(output);
	}

	private void doAllFdNcRequest(long eventid, String userid,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<UserEvent> result = handler.getAllFdNcUserEvent(eventid, userid);          
			printResponse(response, result);	
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void doAllFdRequest(long eventid, String userid,
			HttpServletResponse response) {
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<UserEvent> result = handler.getAllFdUserEvent(eventid, userid);          
			printResponse(response, result);
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
	}

	private void doAllRequest(long eventid, HttpServletResponse response) 
	{
		try {
			DatabaseHandler handler = new DatabaseHandler();
			List<UserEvent> result = handler.getAllUserEvent(eventid);          
			printResponse(response, result);
			handler.closeConnection();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void printResponse(HttpServletResponse response,
			List<UserEvent> result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");           
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        out.write(getOutString(result));
	}

	private String getOutString(List<UserEvent> result) {
		String outstring;
		if (result==null || result.size()==0)
        	outstring = "[]";
        else
        {
        	Gson gson = new Gson();
        	outstring = gson.toJson(result);
        }
		return outstring;
	}
}
